package com.qk.management.statemachine.config.businessconfig;

import com.qk.management.statemachine.event.BusinessEvent;
import com.qk.management.statemachine.status.BusinessStatus;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

import java.util.Arrays;
import java.util.HashSet;

@Configuration
@EnableStateMachineFactory(name = "businessStateMachine")
public class BusinessStateMachineConfig extends EnumStateMachineConfigurerAdapter<BusinessStatus, BusinessEvent> {

    @Override
    public void configure(StateMachineStateConfigurer<BusinessStatus, BusinessEvent> states) throws Exception {
        states.withStates()
                .initial(BusinessStatus.WAIT)
                .states(new HashSet<>(Arrays.asList(BusinessStatus.values())));
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<BusinessStatus, BusinessEvent> transitions) throws Exception {
        transitions
                .withExternal()
                    .source(BusinessStatus.WAIT).target(BusinessStatus.ASSIGNED).event(BusinessEvent.ASSIGN)
                .and()
                .withExternal()
                    .source(BusinessStatus.ASSIGNED).target(BusinessStatus.FOLLOWING).event(BusinessEvent.FOLLOW_UP)
                .and()
                .withExternal()
                    .source(BusinessStatus.FOLLOWING).target(BusinessStatus.FOLLOWING).event(BusinessEvent.FOLLOW_UP) // 可以自循环
                .and()
                .withExternal()
                    .source(BusinessStatus.ASSIGNED).target(BusinessStatus.RECYCLE).event(BusinessEvent.RECYCLE)
                .and()
                .withExternal()
                    .source(BusinessStatus.FOLLOWING).target(BusinessStatus.RECYCLE).event(BusinessEvent.RECYCLE)
                .and()
                .withExternal()
                    .source(BusinessStatus.ASSIGNED).target(BusinessStatus.CUSTOMER).event(BusinessEvent.CONVERT_TO_CUSTOMER)
                .and()
                .withExternal()
                    .source(BusinessStatus.FOLLOWING).target(BusinessStatus.CUSTOMER).event(BusinessEvent.CONVERT_TO_CUSTOMER)
        ;
    }
}